/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.example.demo.util;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	 /**1秒的毫秒值*/
    public static final int ONE_SECOND = 1000;
    /**1分钟的毫秒值*/
    public static final int ONE_MINUTE = 60000;
    /**1小时的毫秒值*/
    public static final int ONE_HOUR = 3600000;
    /**1天的毫秒值*/
    public static final long ONE_DAY = 86400000L;


	/**  yyyy-MM-dd */
	public static final String DEFALUT_DATE_PATTERN="yyyy-MM-dd";
	/**  yyyy-MM-dd HH:mm:ss */
	public static final String DEFALUT_DATETIME_PATTERN="yyyy-MM-dd HH:mm:ss";
	
	public static final String DATETIME_PATTERN_ORDERNUMBER="yyMMddHHmmssSSS";

    /**  yyyy-MM-dd */
    public static final String DEFALUT_DATE_PATTERN2="yyyyMMdd";
	
	/**
	 * 
	 * @param aDate
	 * @param pattern
	 * @return 
	 */
	public static String format(Date aDate,String pattern){
		if(null==aDate)return "";
		 SimpleDateFormat df = new SimpleDateFormat(pattern);
		 return df.format(aDate);
	}
	public static String formatBeginOfDay(Date aDate){
		return formatBeginOfDay(aDate,DEFALUT_DATETIME_PATTERN);
	}
	public static String formatBeginOfDay(Date aDate,String pattern){
		return format(getBeginOfDay(aDate),pattern);
	}
	public static String formatEndOfDay(Date aDate){
		return formatEndOfDay(aDate,DEFALUT_DATETIME_PATTERN);
	}
	public static String formatEndOfDay(Date aDate,String pattern){
		return format(getEndOfDay(aDate),pattern);
	}
	
	/** 
	 * 得到某天的开始时间 凌晨0点0分0秒
	 * @param date 
	 * @return  
	 */
	public static Date getBeginOfDay(Date date){
		if(date==null)	return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}
	
	/** 
	 * 得到某天的结束时间 凌晨23点59分59秒 
	 * @param date
	 * @return 
	 */
	public static Date getEndOfDay(Date date) {
		if(date==null) return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		return cal.getTime();
	}
    /**
     * 得到某日当月月第一天
     * @param date
     * @return
     */
    public static Date getBeginDayOfMonth(int month){
        if(month<0 || month >12) return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.MONTH, month-1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    /**
     * 得到某日当月月最后一天
     * @param date
     * @return
     */
    public static Date getEndDayOfMonth(int month) {
        if(month<0 || month >12) return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.set(Calendar.MONTH, month-1);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    /**
     * 在给定日期上 加减 若干天
     * @param date
     * @param i
     * @return
     */
    public static Date addDay(Date date, int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, i);
        return calendar.getTime();
    }


    public static Date addMonth(Date date, int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, i);
        return calendar.getTime();
    }


    /** 如果需要parse的字符串为空或者空串 返回 null, 转换过程中的异常将被抛出 */
    public static Date nullSafeParse(String strDate){
        return nullSafeParse(strDate, DEFALUT_DATE_PATTERN);
    }

    /** 如果需要parse的字符串为空或者空串 返回 null, 转换过程中的异常将被抛出 */
    public static Date nullSafeParse(String strDate, String pattern){
        if(StringUtil.isEmpty(strDate)){	return null;}

        SimpleDateFormat df =  new SimpleDateFormat(pattern);
        try {
            return df.parse(strDate);
        } catch (Exception e) {
            throw new IllegalArgumentException("字符串 [" + strDate + "] 转换日期错误! pattern [" + pattern + "]");
        }
    }

    /** 得到两个日期中较大者 */
    public static Date getMax(Date date1, Date date2){
        if (date1 == null)
            return date2;
        if (date2 == null)
            return date1;
        if (date1.after(date2))
            return date1;
        return date2;
    }

    /** 得到两个日期中较小者  */
    public static Date getMin(Date date1, Date date2){
        if (date1 == null)
            return date2;
        if (date2 == null)
            return date1;
        if (date1.after(date2))
            return date2;
        return date1;
    }

    /**
     * 返回两个日期直接间隔多少天
     * @param startDate
     * @param stopDate
     * @return
     */
    public static int getBetweenDay(Date startDate,Date stopDate){
        startDate=getBeginOfDay(startDate);
        stopDate=getBeginOfDay(stopDate);
        return (int)((stopDate.getTime() - startDate.getTime())/ONE_DAY);
    }
    /**
     * 返回两个日期实际间隔多少天
     * @param startDate
     * @param stopDate
     * @return
     */
    public static int getBetweenDayActually(Date startDate,Date stopDate){
        int day = (int)((stopDate.getTime() - startDate.getTime())/ONE_DAY);
        if((stopDate.getTime() - startDate.getTime()) % ONE_DAY == 0)
            return day;
        else
            return day+1;
    }


    /**
     * 获得当前年
     * @return
     */
    public static int getNowYear(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        return cal.get(Calendar.YEAR);
    }


    /**
     * 根据生日 返回当前年龄
     * birthday 为null 则返回 -1
     * @param birthday
     * @return
     */
    public static int getAge(Date birthday){
        if(birthday == null){
            return -1;
        }
        Calendar born = Calendar.getInstance();//生日时间
        Calendar now = Calendar.getInstance();//当前时间
        born.setTime(birthday);
        now.setTime(new Date());
        if(born.after(now))
            throw new IllegalArgumentException("生日不能超过当前时间");
        int bornDay = born.get(Calendar.DAY_OF_YEAR);
        int nowDay = now.get(Calendar.DAY_OF_YEAR);
        //考虑到有闰年的情况，如果过了日期过了2.29，则闰年的时间需要减一天来比较。
        int bornYear = born.get(Calendar.YEAR);
        int nowYear = now.get(Calendar.YEAR);
        if(((bornYear%4==0&&bornYear%100!=0)||(bornYear%400==0))&&bornDay>59)
            bornDay--;
        if(((nowYear%4==0&&nowYear%100!=0)||(nowYear%400==0))&&nowDay>59)
            nowDay--;
        int age = nowDay < bornDay ? now.get(Calendar.YEAR) - born.get(Calendar.YEAR)-1 : now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
        return  age;
        //throw new UnsupportedOperationException();
    }
}
